/*
 *  Axolotl - Alternate persistence for Grails
 *  Copyright (C) 2008  Juanjo García Latre
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package com.googlecode.axolotl.query;


public class Query {

	private Class type;
	private Object filter;
	private Object order;
	private Criterion compiledFilter;
	private Order compiledOrder;

	public Query(Class type) {
		this(type, null, null);
	}
	
	
	public Query(Class type, Object filter, Object order) {
		this.type = type;
		this.filter = filter;
		this.order = order;
	}


	public Object getFilter() {
		return filter;
	}


	public void setFilter(Object filter) {
		this.filter = filter;
	}


	public Object getOrder() {
		return order;
	}


	public void setOrder(Object order) {
		this.order = order;
	}


	public Class getType() {
		return type;
	}


	public void setType(Class type) {
		this.type = type;
	}   


	public Criterion getCompiledFilter() {
		if (compiledFilter == null)
			compile();

		return compiledFilter;
	}


	public Order getCompiledOrder() {
		if (compiledOrder == null)
			compile();

		return compiledOrder;
	}


	public void compile() {
		if (filter instanceof Criterion)
			compiledFilter = (Criterion) filter;
		else {
			//@todo
		}

		if (order instanceof Order)
			compiledOrder = (Order) order;
		else {
			//@todo
		}
	}


	/**
	 *
	 */
	public <R> R accept(QueryVisitor<R> visitor) {
		return visitor.visit(this);
	}
	
	
	public String toString() {
		return this.accept(Criterion.STRING_RENDERER).toString();
	}
}
